<!-- 任务分配弹窗 -->

<template>
  <el-dialog :close-on-click-modal="false" :title="$tt('label_sku_pack_box')" :visible.sync="visible"
    custom-class="dialog_big">
    <div v-loading="loading">
      <el-form :label-width="$globalCss.formLabelWidth_default" :model="singlePackForm" ref="packForm" class="mg_t2"
        :rules="BoxPackiRules">
        <el-form-item :label="$tt('label_sku_specification')">
          <ex_super_table v-bind="skuTableOption" :data="singlePackForm.skus" max-height="300" />
          <!-- <div><i class="el-icon-warning color_warning el-icon--left"></i><span class="color_info">
              {{ $tt('tip_add_box_need_select_order_in') }}</span>
          </div> -->
        </el-form-item>
        <div class="flex_row_wrap">
          <el-form-item :label="$tt('label_box_code')" prop="boxNo" class="flex_cell">
            <div class="flex_row">
              <ex_super_input v-model="singlePackForm.boxNo" :disabled="true" class="form_input_small">
              </ex_super_input>
              <el-button type="text" @click="packingGenerate(singlePackForm.inboundOrderCode)" class="mg_l2">
                {{ $tt('label_generate_box_code') }}</el-button>
            </div>
          </el-form-item>
          <el-form-item :label="$tt('label_pack_material')" prop="packagingId" class="flex_cell">
            <pack_material_cascader class="form_input_small" :packId.sync="singlePackForm.packagingId"
              :packName.sync="singlePackForm.packagingName" :typeId.sync="singlePackForm.packagingTypeId"
              :typeName.sync="singlePackForm.packagingTypeName" @changeItem="changePackMaterial" />
          </el-form-item>
        </div>
        <div class="flex_row_wrap">
          <el-form-item :label="$tt('label_weight')" prop="weight" class="flex_cell">
            <ex_super_input v-model="singlePackForm.weight" class="form_input_small" append='kg'>
            </ex_super_input>
          </el-form-item>
          <el-form-item :label="$tt('label_length')" prop="length" class="flex_cell">
            <ex_super_input v-model="singlePackForm.length" class="form_input_small" append='cm'>
            </ex_super_input>
          </el-form-item>
        </div>
        <div class="flex_row_wrap">
          <el-form-item :label="$tt('label_width')" prop="width" class="flex_cell">
            <ex_super_input v-model="singlePackForm.width" class="form_input_small" append='cm'>
            </ex_super_input>
          </el-form-item>
          <el-form-item :label="$tt('label_height')" prop="height" class="flex_cell">
            <ex_super_input v-model="singlePackForm.height" class="form_input_small" append='cm'>
            </ex_super_input>
          </el-form-item>
        </div>

        <el-form-item :label="$tt('label_inner_sku')">
          <el-row>
            <el-col :span="10">
              <ex_super_input v-model="packTempBoxNumber" v-on:keyup.native="packingBoxAdd"></ex_super_input>
            </el-col>
            <el-col :span="6" :offset="1">
              <el-button type="primary" @click="packingBoxAdd">
                {{ $tt('label_scan') }}</el-button>
            </el-col>
          </el-row>
        </el-form-item>

        <el-form-item prop="beBoxList" :label="$tt('label_packbox_list')">
          <ex_super_table :data="singlePackForm.beBoxList" v-bind="BoxTableOption" />
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog_footer">
        <el-button @click="visible = false">{{ $tt('label_cancel') }}</el-button>
        <el-button type="primary" @click="submitPack">{{ $tt('label_submit') }}</el-button>
      </span>
    </div>

  </el-dialog>
</template>

<script>
import pack_material_cascader from '@/component/wms/pack_material_cascader.vue';
import select_file_temp from '@/component/base/select_file_temp'
import Schema from 'async-validator'
const default_dialogPackingForm = {
  skus: [],
  packMaterialWeight: 0,
  packagingTypeId: "",//	long 	包材类型ID
  packagingTypeName: "",//		string 	包材类型名称
  packagingId: "",//	long 	包材名称ID
  packagingName: "",//	string 	包材名称
  boxNo: "",//	string 	箱号
  length: "",//	bigdecimal 	长(CM)
  width: "",//	bigdecimal 	宽(CM)
  height: "",//	bigdecimal 	高(CM)
  weight: "",//	bigdecimal 	重量(KG)
  volume: "",//	bigdecimal 	体积
  beBoxList: [],//	array<object> 	beBoxList
  inboundOrderId: "",//	long 	入库单ID 
}
export default {
  components: { pack_material_cascader, select_file_temp, },
  data() {
    return {
      activeTab: 'single',
      visible: false,

      loading: false,

      packTempBoxNumber: "",
      selectOrderList: [],
      singlePackForm: _.cloneDeep(default_dialogPackingForm),
      skuTableOption: {
        attrs: { stripe: true, },
        defaultColAttr: {
          align: 'left',
        },
        model: [
          { key: 'skuName', label: 'label_sku_info', 'min-width': 120, 'show-overflow-tooltip': true, },
          {
            key: 'skuBarcode', label: 'label_sku_code', 'min-width': 120, 'show-overflow-tooltip': true,
            propertyFilter: true, sortable: true,
          },
          {
            key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 220,
            propertyFilter: true, sortable: true,
          },
          {
            key: 'quantity', label: 'label_expected_sku_number', 'width': 110,
            sortable: true,
          },
          {
            key: 'receivingQty', label: 'label_receiving_number', 'width': 110,
            sortable: true,
          },
          {
            key: 'acceptanceQty', label: 'label_acceptance_number', 'width': 110,
            sortable: true,
          },
          {
            key: 'packingBoxQty', label: 'label_packbox_count', 'width': 110,
            sortable: true,
          },
          {
            key: 'canOperateCount', label: 'label_can_operate_count', 'width': 120,
            sortable: true,
          },
          {
            type: 'operate', width: 180, fixed: "right", operate: [
              {
                label: "label_add_to_packbox", disabled: (row) => !row.canOperateCount,
                click: (row) => this.addBox(row)

              },

            ]
          }
        ],
      },

      BoxTableOption: {
        height: 250,
        model: [
          { key: 'skuName', label: 'label_sku_info', 'width': 120, 'show-overflow-tooltip': true, },
          {
            key: 'skuBarcode', label: 'label_sku_code', 'width': 120, 'show-overflow-tooltip': true,
            propertyFilter: true, sortable: true,
          },
          {
            key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 220,
            propertyFilter: true, sortable: true,
          },
          // {
          //   key: 'skuOnlyCode', label: 'label_sku_only_code', 'min-width': 220,
          //   propertyFilter: true, sortable: true,
          // },
          {
            key: 'canOperateCount', label: 'label_can_operate_count', 'width': 120,
            sortable: true,
          },
          {
            key: 'packingBoxQty', label: 'label_packbox_count', 'min-width': 140,
            sortable: true,
            render: (h, { row, index, column }) => {
              const value = row[column.property]
              return (
                <el-form-item
                  prop={`beBoxList.${index}.${column.property}`}

                >
                  <el-input-number
                    value={value}
                    class='w_fill'
                    min={1}
                    max={row.canOperateCount}
                    onInput={e => {
                      this.$set(row, column.property, e)
                      this.refreshWeight()
                    }}
                    placeholder={column.label}
                  >
                  </el-input-number>
                </el-form-item>
              )
            },
          },
          {
            type: 'operate', width: 160, operate: [
              {
                label: "label_remove", class: 'button_text_danger',
                click: (row, i) => {
                  this.singlePackForm.beBoxList.splice(i, 1)
                  this.refreshWeight()
                }
              },
            ]
          }
        ]
      },
      BoxPackiRules: {
        packagingTypeId: [{ required: true, message: this.$tt('packagingType_validate_error'), trigger: 'change' }],
        packagingId: [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
        packageNo: [{ required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],

        beBoxList: [{ type: 'array', required: true, message: this.$tt('tip_can_no_empty'), trigger: 'change' }],
        length: {
          required: true,
          validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 3, 9)) {
              return cb(this.$tt("tip_check_decimal_digit_int", { digit: 3, int: 9 }))
            }
            cb()
          }, trigger: 'change',
        },
        width: {
          required: true,
          validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 3, 9)) {
              return cb(this.$tt("tip_check_decimal_digit_int", { digit: 3, int: 9 }))
            }
            cb()
          }, trigger: 'change'
        },
        height: {
          required: true,
          validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 3, 9)) {
              return cb(this.$tt("tip_check_decimal_digit_int", { digit: 3, int: 9 }))
            }
            cb()
          }, trigger: 'change'
        },
        weight: {
          required: true,
          validator: (r, v, cb) => {
            if (!this.$reg.DigitNumber(v, 3, 9)) {
              return cb(this.$tt("tip_check_decimal_digit_int", { digit: 3, int: 9 }))
            }
            cb()
          }, trigger: 'change'
        },
      },

    }
  },
  props: {

  },
  methods: {
    changePackMaterial(e) {
      if (e) {
        this.$set(this.singlePackForm, 'length', e.length)
        this.singlePackForm.width = e.width
        this.singlePackForm.height = e.height
        // this.singlePackForm.weight = e.weight
        this.singlePackForm.packMaterialWeight = e.weight
        this.refreshWeight()
      }
    },

    refreshWeight() {
      const sumW = _.reduce(this.singlePackForm.beBoxList,
        (sum, it) => this.$math_add(sum, this.$math_mul(it.skuWeight, it.packingBoxQty)), 0)

      this.singlePackForm.weight = this.$math_add(sumW, this.singlePackForm.packMaterialWeight)
    },
    addBox(row) {
      const item = _.find(this.singlePackForm.beBoxList, it => it.skuId == row.skuId)
      if (item) {
        if (item.packingBoxQty < item.canOperateCount) {
          this.$set(item, 'packingBoxQty', item.packingBoxQty + 1)
          this.refreshWeight()
        }
      } else {
        const will = _.cloneDeep(row)
        this.$set(will, 'packingBoxQty', 1)
        this.singlePackForm.beBoxList.push(will)
        this.refreshWeight()
      }
    },

    packingGenerate(code) {
      this.$ajax({
        url: "/wms-api/inbound/WmsInboundTransitReceivingRpc/generateBatchBoxNo.json",
        method: 'post', loading: true,
        data: { number: 1, inboundOrderCode: code },
        success: (res) => {
          let code = null
          if (res && res.data && res.data.rows) {
            code = _.head(res.data.rows)
          }
          this.singlePackForm.boxNo = code
        }
      })
    },

    show({ order }) {


      // this.singlePackForm = _.cloneDeep(default_dialogPackingForm)
      // this.singlePackForm.receivingIdList = _list.map(it => it.id)
      // this.selectOrderList = _list

      this.visible = true
      this.loading = true
      this.$ajax({
        url: "/wms-api/inbound/WmsInboundTransitPackagingRpc/selectPackingBoxDetail.json",
        method: 'post',
        data: {
          inboundOrderId: order.id,
        },
        success: (res) => {
          if (res && res.data && res.data.wmsInboundInboundOrderSkuList) {

            this.singlePackForm = _.cloneDeep(default_dialogPackingForm)
            this.singlePackForm.inboundOrderId = order.id
            this.singlePackForm.inboundOrderCode = order.code
            this.singlePackForm.skus = res.data.wmsInboundInboundOrderSkuList.map(it => {
              it.canOperateCount = 0//it.receivingQty
              if (it.acceptanceStatus == 'ACCEPTANCE_COMPLETED') {
                it.canOperateCount = it.acceptanceQty - it.packingBoxQty
              }
              return it
            })
            this.packingGenerate(this.singlePackForm.inboundOrderCode)
          }
        },
        complete: () => {
          this.loading = false
        }
      })

    },

    submitPackNet() {
      const data = _.cloneDeep(this.singlePackForm)

      data.volume = this.$math_ceil(this.$math_mul(
        this.$math_mul(0.01,this.singlePackForm.length),
        this.$math_mul(0.01, this.singlePackForm.width),
        this.$math_mul(0.01,this.singlePackForm.height),
        0.000001
      ), 8)
      data.skuList = data.beBoxList.map(it => ({
        boxDetailId: it.id,
        packingBoxQty: it.packingBoxQty
      }))
      this.$ajax({
        url: "/wms-api/inbound/WmsInboundTransitPackagingRpc/packingBox.json",
        method: 'post', type: 'json', loading: true,
        data: data,
        success: (res) => {
          this.$message.success(this.$tt('tip_submit_success'))
          this.$emit('success', true)
          this.visible = false
        }
      })
    },



    submitPack() {
      if (this.$clickFast()) return
      this.$refs['packForm'].validate((valid) => {
        if (valid) {
          this.submitPackNet()
        } else {
          this.$message.error(this.$tt('form_validate_error'))
          return false;
        }
      });

    },

    packingBoxAdd() {
      if (_.isEmpty(this.packTempBoxNumber)) {
        return this.$message.error(this.$tt("tip_type_can_no_empty", { type: "label_sku_only_code", i18n: true }))
      }
      const item = _.find(this.singlePackForm.skus, it => it.skuOnlyCode == this.packTempBoxNumber)
      if (item) {
        this.addBox(item)
      } else {
        return this.$message.error(this.$tt("tip_not_found_data"))
      }
    },

  }
}
</script>
